package com.example.camerafacedetect.faceframe;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.media.FaceDetector;
import android.util.Log;

import androidx.annotation.NonNull;

import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.mlkit.vision.common.InputImage;
import com.google.mlkit.vision.face.Face;
import com.google.mlkit.vision.face.FaceDetection;
import com.google.mlkit.vision.face.FaceDetectorOptions;

import java.util.List;

/**
 * Created by yubo on 2015/9/5.
 * 人脸检测工具类
 */
public class FaceDetectorUtils {

    private static FaceDetector faceDetector;

    private FaceDetectorUtils(){
    }

    public interface Callback{
        void onFaceDetected(Bitmap bitmap);
        void onFaceNotDetected(Bitmap bitmap);
    }

    private static int dectect_counter = 0;
    /**
     * 检测bitmap中的人脸，在callback中返回人脸数据
     * @param bitmap
     * @param callback
     */
    public static void detectFace(Bitmap bitmap, Callback callback){
        try {
            dectect_counter++;
            // 每隔2帧，就不检测一张人脸
            if(dectect_counter == 2){
                callback.onFaceDetected(bitmap);
                dectect_counter=0;

                return;
            }
            InputImage image = InputImage.fromBitmap(bitmap, 0);
            FaceDetectorOptions realTimeOpts =
                    new FaceDetectorOptions.Builder()
                            .setContourMode(FaceDetectorOptions.CONTOUR_MODE_ALL)
                            .build();
            com.google.mlkit.vision.face.FaceDetector detector = FaceDetection.getClient(realTimeOpts);
            Task<List<com.google.mlkit.vision.face.Face>> result =
                    detector.process(image)
                            .addOnSuccessListener(
                                    new OnSuccessListener<List<Face>>() {
                                        @Override
                                        public void onSuccess(List<Face> faces) {
                                            Bitmap mutableBitmap = painFaceFrame(faces, bitmap);
                                            callback.onFaceDetected(mutableBitmap);
                                        }
                                    })
                            .addOnFailureListener(
                                    new OnFailureListener() {
                                        @Override
                                        public void onFailure(@NonNull Exception e) {
                                            Log.e("++++++++", "face on detct onFailure: " + e.getStackTrace());
                                        }
                                    });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Bitmap painFaceFrame(List<Face> faces, Bitmap bitmap) {
        Bitmap retBitmap = null;
        // 不要FaceView加载后再画框，而是直接在获取到图片后添加上人脸框，
        if(faces != null && faces.size() > 0) {
            // TODO 这里copy处理处理可能是冗余，考虑直接干掉，不过可能因为Canvas必须要要是isMutalbe才可以
            retBitmap = bitmap.copy(Bitmap.Config.RGB_565, true);
            Canvas canvas = new Canvas(retBitmap);
            Paint paint = new Paint();
            paint.setColor(Color.GREEN); // 颜色
            paint.setStyle(Paint.Style.STROKE); // 非实心图，即框图
            paint.setStrokeWidth(2.0f); // 线宽
            Face face = faces.get(0);
            Rect rect = face.getBoundingBox();
            canvas.drawRect(rect.left, rect.top, rect.right, rect.bottom, paint);
        }else{
            retBitmap = bitmap;
        }

        return retBitmap;
    }

}